﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;

public partial class Login : System.Web.UI.Page
{
    security baomat = new security();
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            HttpCookie _user = Request.Cookies["userinfo"];
            if (Request.Cookies["userinfo"] != null)
            {
                if (_user["username"] == null)
                {
                    Response.RedirectToRoute("Index");
                }
            }
            else
                Response.RedirectToRoute("Index");
        }
        catch (Exception ex) { }
    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        try
        {
            HttpCookie _user = Request.Cookies["userinfo"];
            if (Request.Cookies["userinfo"] != null)
            {
                using (Up1VNDataContext dc = new Up1VNDataContext())
                {
                    string currentPWD = baomat.EncryptPWD(txtcurrentPWD.Text);
                    var checkPassword = from o in dc.Users
                                        where o.Users_Name == _user["username"] && o.Users_Password == currentPWD
                                        select o;
                    if (checkPassword.Count() == 0)
                        lblError.Text = "Mật khẩu hiện tại không đúng";
                    else
                    {
                        User U = dc.Users.Single(c => c.Users_Name == _user["username"]);
                        U.Users_Password = baomat.EncryptPWD(txtnewpassword.Text);
                        dc.SubmitChanges();
                        lblError.Text = "Đổi thành công mật khẩu";
                        Thread.Sleep(5000);
                        Response.RedirectToRoute("ViewUser");
                    }
                }
            }
        }
        catch (Exception ex)
        {
        }
    }
}